/**
 * http://www.jootu.net
 */
package com.jootu.cc.ccapi.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jootu.cc.ccapi.domain.resultmap.JsonResult;
import com.jootu.cc.ccapi.domain.resultmap.PhoneNumber;
import com.jootu.cc.ccapi.domain.resultmap.ResultCode;
import com.jootu.cc.ccapi.domain.resultmap.UserInfo;
import com.jootu.cc.ccapi.service.AttachmentService;
import com.jootu.cc.ccapi.service.UserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author:antch
 * @date:2017年8月10日
 * @Description:（用户信息处理控制器）
 *
 */
@Api(value = "用户信息控制器")
@RestController
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private AttachmentService attachmentService;

	@ApiOperation(value = "个人信息", notes = "根据url中的id获取用户详细信息")
	@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/getuser/{id}", method = RequestMethod.GET)
	public UserInfo getUserInfoById(@PathVariable("id") Integer id) throws Exception {
		if (id == 11) {
			throw new Exception();
		}
		return id == null ? null : userService.getUserInfoById(id);
	}

	@ApiOperation(value = "通讯录", notes = "通讯录按先监工，后施工人员分组，每个组里按姓名排序，最终合成一个List")
	@RequestMapping(value = "/getphonelist", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> getPhoneNumberList() {
		long startTime = System.currentTimeMillis();//
		JsonResult j = new JsonResult();
		try {
			List<PhoneNumber> lists = userService.getPhoneNumberList();
			j.setCode(ResultCode.SUCCESS);
			j.setMess(ResultCode.SUCCESS_MEG);
			j.setResult(lists);
			j.setProcessTime(System.currentTimeMillis() - startTime);
			return ResponseEntity.ok(j);

		} catch (Exception e) {
			// TODO: handle exception

			j.setCode(-2);
			j.setMess("服务端错误");
			j.setResult(null);
			j.setProcessTime(System.currentTimeMillis() - startTime);
		}
		return ResponseEntity.ok(j);
	}

	@ExceptionHandler(Exception.class)
	public ResponseEntity<JsonResult> exceptionHandle(HttpRequest request, Exception e) {
		JsonResult j = new JsonResult();
		j.setCode(ResultCode.PARAMS_ERROR);
		j.setMess(ResultCode.PARAMS_ERROR_MEG);
		j.setResult(null);
		return ResponseEntity.ok(j);
	}

	@ApiOperation(value = "分页插件测试", notes = "分页插件测试")
	// @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "当前页",
	// required = true, dataType = "Integer", paramType = "path")
	// ,@ApiImplicitParam(name = "pageSize", value = "当前页数据量", required = true,
	// dataType = "Integer", paramType = "path")})
	@RequestMapping(value = "/getattachment", method = RequestMethod.GET)
	public ResponseEntity<JsonResult> getAttachmentByPage(@RequestParam(name = "pageNum") int pageNum,
			@RequestParam(name = "pageSize") int pageSize) {
		long startTime = System.currentTimeMillis();
		pageNum = 1;
		pageSize = 3;
		JsonResult j = new JsonResult();
		try {
			System.out.println("分页-----》》》》》》》》》》");
			j.setResult(attachmentService.getAttachmentsByPage(pageNum, pageSize));
			System.out.println("分页-----》》》》》》》》》》");
			j.setCode(ResultCode.SUCCESS);
			j.setMess(ResultCode.SUCCESS_MEG);
			j.setResult(attachmentService.getAttachmentsByPage(pageNum, pageSize));
			j.setProcessTime(System.currentTimeMillis() - startTime);
			return ResponseEntity.ok(j);
		} catch (Exception e) {
			e.printStackTrace();
			j.setCode(ResultCode.SYS_ERROR);
			j.setMess("服务器错误");
			j.setProcessTime(System.currentTimeMillis() - startTime);
			j.setResult(null);
			return ResponseEntity.ok(j);
		}
	}
}
